package com.tony.activotytest;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends BaseActivity {

    private Button button1;
    private  Button btn_startNormalActivity,btn_StartDialogActivity,btnSimpleUIComponent;
    private static final String TAG = "MainActivity";



    @Override //重写
    protected void onCreate(Bundle savedInstanceState) {//第一次被创建onCreate
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//页面activity_main

        //自定义方法
        buttonInitView();
        Log.d(TAG,"onCreate(开始创建)");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"onStart(开始加载)");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"onResume(继续)");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"onPause(暂停)");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"onStop(停止)");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy(销毁)");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) { //创建menu
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {//选中菜单
        switch (item.getItemId()){
            case R.id.add_item :
                Toast.makeText(this, "点击新增", Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_item:
                Toast.makeText(this, "点击了移除", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //初始化视图
    public void buttonInitView(){
        button1 = findViewById(R.id.button1);

        btn_startNormalActivity = findViewById(R.id.btn_start_normal_activity);
        btn_StartDialogActivity = findViewById(R.id.btn_start_dialog_activity);

        btn_startNormalActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,NormalActivity.class);
            startActivity(intent);
            }
        });

        btn_StartDialogActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,DialogActivity.class);
                startActivity(intent);
            }
        });

        btnSimpleUIComponent = findViewById(R.id.btn_simple_uicomponent);
        btnSimpleUIComponent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转页面
                Intent intent = new Intent(MainActivity.this,SimpleComponentActivity.class);
                startActivity(intent);
            }
        });


        //为按钮添加监视器
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //onClick会在button1按钮按下时自动调用
//                Toast.makeText(MainActivity.this, "点击我了", Toast.LENGTH_SHORT).show();

                //从第一个窗体跳转到第二个(显式意图)
//                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
//                startActivity(intent);

                //隐式方式
//                Intent intent = new Intent("com.androidstudy.activitytest.START_ACTION");
//                startActivity(intent);

                // 通过隐式Intent调用系统应用
                Intent intent = new Intent();
//                打电话
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:17621930966"));
                startActivity(intent);

//               系统设置
//                intent.setAction("android.settings.SETTINGS");


                //传参
//                Intent intent = new Intent("com.androidstudy.activitytest.START_ACTION");
////                intent.putExtra("Data","Hello world");
////                startActivity(intent);
//                // 反向传参
//                startActivityForResult(intent,1);

            }
        });
    }


    //返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if(resultCode == RESULT_OK){
                    String resultData = data.getStringExtra("data_return");
                    Log.d("MainActivity",resultData);
                }
                break;

        }
    }







}